
package cn.nubia.admin.application.bean;

import cn.nubia.admin.application.apk.entity.ApkInfo;
import cn.nubia.admin.common.constant.PicSizeEnum;
import cn.nubia.admin.common.mybatis.annotations.Column;
import cn.nubia.admin.common.mybatis.annotations.Id;
import cn.nubia.admin.common.mybatis.annotations.Table;
import cn.nubia.admin.common.mybatis.annotations.UnColumn;
import cn.nubia.admin.file.bean.Upload;
import org.hibernate.validator.constraints.NotEmpty;

import java.io.File;
import java.io.Serializable;

/**
 * Created by justin on 15/6/30.
 * CREATE TABLE `nb_client_version` (
 * `client_id` int(11) NOT NULL DEFAULT '0' COMMENT '客户端id',
 * `client_name` varchar(100) DEFAULT NULL COMMENT '客户端名称',
 * `package_name` varchar(100) DEFAULT NULL COMMENT '包名',
 * `version_name` varchar(100) DEFAULT NULL COMMENT '版本名称',
 * `version_code` int(11) DEFAULT NULL COMMENT '版本号',
 * `firmware` varchar(50) DEFAULT NULL COMMENT '固件版本',
 * `client_type` smallint(6) DEFAULT NULL COMMENT '客户端类型（0-通用1-定制）',
 * `resolution` varchar(20) DEFAULT NULL COMMENT '屏幕分辨率',
 * `update_log` longtext COMMENT '更新说明',
 * `icon_url` varchar(255) DEFAULT NULL COMMENT '图标路径',
 * `download_url` varchar(255) DEFAULT NULL COMMENT '文件下载地址',
 * `download_number` int(11) DEFAULT NULL COMMENT '下载总数',
 * `total_number` int(11) DEFAULT NULL COMMENT '不同版本的下载总数',
 * `phone_model` varchar(50) DEFAULT NULL COMMENT '厂商的特定机型',
 * `minsdk` smallint(6) DEFAULT NULL COMMENT '最低固件版本',
 * `maxsdk` smallint(6) DEFAULT NULL COMMENT '最高固件版本',
 * `filesize` int(11) DEFAULT NULL COMMENT '文件大小',
 * `checknum` varchar(50) DEFAULT NULL COMMENT 'MD5值',
 * `state` smallint(6) DEFAULT NULL COMMENT '状态',
 * `qrcode_url` varchar(100) DEFAULT NULL COMMENT '二维码地址',
 * `publish_time` int(11) DEFAULT NULL COMMENT '上架时间(时间戳)',
 * `create_time` int(11) DEFAULT NULL COMMENT '创建时间(时间戳)',
 * `create_admin_id` int(11) DEFAULT NULL COMMENT '添加的管理员id',
 * `last_modify_time` int(11) DEFAULT NULL COMMENT '最后修改时间(时间戳)',
 * `is_last_version` smallint(6) DEFAULT NULL COMMENT '是否是最新版本（0-不是 1-是）',
 * `language` int(11) DEFAULT NULL COMMENT '语言id',
 * `channel_id` char(10) DEFAULT NULL COMMENT '渠道商编号',
 * PRIMARY KEY (`client_id`)
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='手机客户端版本';
 */
@Table(value = "nb_client_version")
public class ClientVersion implements Serializable {

    private static final long serialVersionUID = -8244661101951482489L;

    public static final int STATE_DEL = -1;
    public static final int STATE_UP = 1;
    public static final int STATE_SAVE = 0;


    public static final String COLUMN_STATE = "state";
    public static final String COLUMN_PUBLISHTIME = "publishTime";
    public static final String COLUMN_CREATETIME = "createTime";
    public static final String COLUMN_CHANNELID = "channelId";
    public static final String COLUMN_CLIENTTYPE = "clientType";


    @Id(value = "client_id")
    private Integer id;
    @NotEmpty(message = "{client.clientName.null}")
    @Column(value = "client_name")
    private String clientName;
    @NotEmpty(message = "{client.packageName.null}")
    @Column(value = "package_name")
    private String packageName;//包名
    @NotEmpty(message = "{client.versionName.null}")
    @Column(value = "version_name")
    private String versionName;//版本名称
    @Column(value = "version_code")
    private Integer versionCode;//版本号
    @Column(value = "firmware")
    private String firmware;//固件版本
    @Column(value = "client_type")
    private Integer clientType;//客户端类型（0-通用1-定制）
    @Column(value = "resolution")
    private Integer resolution;//屏幕分辨率
    @Column(value = "update_log")
    private String updateLog;//更新说明
    @Column(value = "icon_url")
    private String iconUrl;//图标路径
    @Column(value = "download_url")
    private String downloadUrl;//文件下载地址
    @Column(value = "download_number")
    private Integer downloadNumber;//下载总数
    @Column(value = "total_number")
    private Integer totalNumber;//不同版本的下载总数
    @Column(value = "phone_model")
    private String phoneModel;//厂商的特定机型
    @Column(value = "minsdk")
    private Integer minsdk;//最低固件版本
    @Column(value = "maxsdk")
    private Integer maxsdk;//最高固件版本
    @Column(value = "filesize")
    private Integer filesize;//文件大小
    @Column(value = "checknum")
    private String checknum;//MD5值
    @Column(value = "state")
    private Integer state;//状态-1 删除   0 保存  1 发布
    @Column(value = "qrcode_url")
    private String qrcodeUrl;//二维码地址
    @Column(value = "publish_time")
    private Integer publishTime;//上架时间(时间戳)
    @Column(value = "create_time")
    private Integer createTime;//创建时间(时间戳)
    @Column(value = "create_admin_id")
    private Integer createAdminId;//添加的管理员id
    @Column(value = "last_modify_time")
    private Integer lastModifyTime;//最后修改时间(时间戳)
    @Column(value = "is_last_version")
    private Integer isLastVersion;//是否是最新版本（0-不是 1-是）
    @Column(value = "language")
    private Integer language;//语言id
    @Column(value = "channel_id")
    private Integer channelId;//渠道商编号
    @UnColumn
    private String resolutionShow;
    @UnColumn
    private String minsdkName;//最低固件版本
    @UnColumn
    private String maxsdkName;//最高固件版本
    @UnColumn
    private String icon512Url;
    @UnColumn
    private String icon125Url;
    @UnColumn
    private String icon114Url;
    @UnColumn
    private String icon62Url;
    @UnColumn
    private String publishTimeShow;



    public void copyStakeProperties(ClientVersion clientVersion){
        this.setCreateAdminId(clientVersion.getCreateAdminId());
        this.setCreateTime(clientVersion.getCreateTime());
    }

    public ClientVersion initApkInfo(ApkInfo apkInfo) {
        this.setPackageName(apkInfo.getPackageName());
        if(apkInfo.getVersionCode()!=null)this.setVersionCode(Integer.valueOf(apkInfo.getVersionCode()));
        this.setVersionName(apkInfo.getVersionName());
        this.setClientName(apkInfo.getApplicationLable());
        String userPermistions = "";
        for(String permission : apkInfo.getUsesPermissions()){
            userPermistions += permission + ";";
        }
        if(apkInfo.getMinSdkVersion() != null){
            this.setMinsdk(Integer.valueOf(apkInfo.getMinSdkVersion()));
        }else {
            if(apkInfo.getSdkVersion() != null){
                this.setMinsdk(Integer.valueOf(apkInfo.getSdkVersion()));
            }
        }
        if(apkInfo.getMaxSdkVersion()!=null){
            this.setMaxsdk(Integer.valueOf(apkInfo.getMaxSdkVersion()));
        }else{
            if(apkInfo.getTargetSdkVersion()!=null){
                this.setMaxsdk(Integer.valueOf(apkInfo.getTargetSdkVersion()));
            }
        }
        this.setChecknum(apkInfo.getFileMD5());
        this.setFilesize(apkInfo.getFileSize());
        String supportsScreens = apkInfo.getSupportsScreens();
        int ret = 0;
        if (supportsScreens.contains("small"))
        {
            ret = ret|2;
        }
        if (supportsScreens.contains("normal"))
        {
            ret = ret|4;
        }
        if (supportsScreens.contains("large"))
        {
            ret = ret|8;
        }
        if (supportsScreens.contains("xlarge"))
        {
            ret = ret|16;
        }
        this.setResolution(ret);
        return this;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public Integer getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(Integer versionCode) {
        this.versionCode = versionCode;
    }

    public String getFirmware() {
        return firmware;
    }

    public void setFirmware(String firmware) {
        this.firmware = firmware;
    }

    public Integer getClientType() {
        return clientType;
    }

    public void setClientType(Integer clientType) {
        this.clientType = clientType;
    }

    public Integer getResolution() {
        return resolution;
    }

    public void setResolution(Integer resolution) {
        this.resolution = resolution;
    }

    public String getUpdateLog() {
        return updateLog;
    }

    public void setUpdateLog(String updateLog) {
        this.updateLog = updateLog;
    }

    public String getIconUrl() {
        return iconUrl;
    }

    public void setIconUrl(String iconUrl) {
        this.iconUrl = iconUrl;
        if(this.iconUrl!=null||!"".equals(this.iconUrl)){
            int location = iconUrl.lastIndexOf(File.separator);
            if(location>0){
                String iconPath = iconUrl.substring(0, location);
                this.setIcon512Url(iconPath + File.separator + "icon" + Upload.FILE_DIV + PicSizeEnum.ICON_512.getSize() + ".png");
                this.setIcon125Url(iconPath + File.separator + "icon" +Upload.FILE_DIV + PicSizeEnum.ICON_125.getSize() + ".png");
                this.setIcon114Url(iconPath + File.separator + "icon" +Upload.FILE_DIV + PicSizeEnum.ICON_114.getSize() + ".png");
                this.setIcon62Url(iconPath + File.separator + "icon" +Upload.FILE_DIV + PicSizeEnum.ICON_62.getSize() + ".png");
            }
        }

    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public Integer getDownloadNumber() {
        return downloadNumber;
    }

    public void setDownloadNumber(Integer downloadNumber) {
        this.downloadNumber = downloadNumber;
    }

    public Integer getTotalNumber() {
        return totalNumber;
    }

    public void setTotalNumber(Integer totalNumber) {
        this.totalNumber = totalNumber;
    }

    public String getPhoneModel() {
        return phoneModel;
    }

    public void setPhoneModel(String phoneModel) {
        this.phoneModel = phoneModel;
    }

    public Integer getMinsdk() {
        return minsdk;
    }

    public void setMinsdk(Integer minsdk) {
        this.minsdk = minsdk;
    }

    public Integer getMaxsdk() {
        return maxsdk;
    }

    public void setMaxsdk(Integer maxsdk) {
        this.maxsdk = maxsdk;
    }

    public Integer getFilesize() {
        return filesize;
    }

    public void setFilesize(Integer filesize) {
        this.filesize = filesize;
    }

    public String getChecknum() {
        return checknum;
    }

    public void setChecknum(String checknum) {
        this.checknum = checknum;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getQrcodeUrl() {
        return qrcodeUrl;
    }

    public void setQrcodeUrl(String qrcodeUrl) {
        this.qrcodeUrl = qrcodeUrl;
    }

    public Integer getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(Integer publishTime) {
        this.publishTime = publishTime;
    }

    public Integer getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Integer createTime) {
        this.createTime = createTime;
    }

    public Integer getCreateAdminId() {
        return createAdminId;
    }

    public void setCreateAdminId(Integer createAdminId) {
        this.createAdminId = createAdminId;
    }

    public Integer getLastModifyTime() {
        return lastModifyTime;
    }

    public void setLastModifyTime(Integer lastModifyTime) {
        this.lastModifyTime = lastModifyTime;
    }

    public Integer getIsLastVersion() {
        return isLastVersion;
    }

    public void setIsLastVersion(Integer isLastVersion) {
        this.isLastVersion = isLastVersion;
    }

    public Integer getLanguage() {
        return language;
    }

    public void setLanguage(Integer language) {
        this.language = language;
    }

    public Integer getChannelId() {
        return channelId;
    }

    public void setChannelId(Integer channelId) {
        this.channelId = channelId;
    }

    public String getResolutionShow() {
        return resolutionShow;
    }

    public void setResolutionShow(String resolutionShow) {
        this.resolutionShow = resolutionShow;
    }

    public String getMinsdkName() {
        return minsdkName;
    }

    public void setMinsdkName(String minsdkName) {
        this.minsdkName = minsdkName;
    }

    public String getMaxsdkName() {
        return maxsdkName;
    }

    public void setMaxsdkName(String maxsdkName) {
        this.maxsdkName = maxsdkName;
    }

    public String getIcon512Url() {
        return icon512Url;
    }

    public void setIcon512Url(String icon512Url) {
        this.icon512Url = icon512Url;
    }

    public String getIcon125Url() {
        return icon125Url;
    }

    public void setIcon125Url(String icon125Url) {
        this.icon125Url = icon125Url;
    }

    public String getIcon114Url() {
        return icon114Url;
    }

    public void setIcon114Url(String icon114Url) {
        this.icon114Url = icon114Url;
    }

    public String getIcon62Url() {
        return icon62Url;
    }

    public void setIcon62Url(String icon62Url) {
        this.icon62Url = icon62Url;
    }

    public String getPublishTimeShow() {
        return publishTimeShow;
    }

    public void setPublishTimeShow(String publishTimeShow) {
        this.publishTimeShow = publishTimeShow;
    }
}
